var map;
      var infowindow;
      var markersArray = [];
      
	var futionTableLayer = null;
	var FUSIONTABLEID = "2335669";
	var lastClickPoint = null;
      function initialize() {
        var pyrmont = new google.maps.LatLng(39.932907,116.400146);

        map = new google.maps.Map(document.getElementById('map'), {
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center: pyrmont,
          zoom: 15
        });
        futionTableLayer = new google.maps.FusionTablesLayer(FUSIONTABLEID);
        document.getElementById("searchimg").onclick = search;
      }

      function search(){
      	var pyrmont = map.getCenter();
      	var keyword = document.getElementById("searchKeyword").value;
      	var request = {
          location: pyrmont,
          radius: 1000,
          keyword :keyword,
          types: ['restaurant','food','cafe','bakery','meal delivery','meal takeaway','lodging']
        };
        infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);
        service.search(request, callback);
      }
      function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
        	clearMarkers();
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }

      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });
		markersArray.push(marker);
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
      }
      
      function clearMarkers(){
      	if (markersArray) {
		    for (i in markersArray) {
		      markersArray[i].setMap(null);
		    }
		  }
		 markersArray.length=0;
      }
      
      function showOverlays() {
		  if (markersArray) {
		    for (i in markersArray) {
		      markersArray[i].setMap(map);
		    }
		  }
		}

      google.maps.event.addDomListener(window, 'load', initialize);
      
      function typeSearch(){
      	var type = $(window.event.srcElement).html();
      	var temp = $(window.event.srcElement).attr('class');
      	var queryInfo = {type:temp,value:['酒店',type]};
      	var url = "operator";
	  	url = encodeURI(url);
	  	var queryInfo = encodeURI(JSON.stringify(queryInfo));
		$.post(
		 	url,
		 	{
		 		 method : "query",
		 		 queryInfo : queryInfo
		 	},
		 	function(response){
		 		alert(response);
		 	}
		 );
//      	var query = "select * from "+FUSIONTABLEID+" where "+temp+"='"+type+"'";
//      	futionTableLayer.setQuery(query);
//      	futionTableLayer.setMap(map);
      }
      
      function addPoint(){
      	var table = new FusionTable(FUSIONTABLEID,"yumeng5566@gmail.com","yoyo3561122");
	  	table.querySomething();
      	var addPointListener = google.maps.event.addListener(map, 'click', function(event) {
		    google.maps.event.removeListener(addPointListener);
		    lastClickPoint = event.latLng
		    _addPointMarker();
		});
      }
      
      function _addPointMarker(){
      	
      	var marker = new google.maps.Marker({
	        position: lastClickPoint, 
	        map: map
    	});
    	var rootDiv = $("<div>");
    	var fields = {name:'名称',maintype:'类别', subtype:'子类别', vicinity:'地址'}
    	for (var fieldName in fields) {
    		$("<div class='field' fieldName='"+fieldName+"'>"+fields[fieldName]+":<input type='text'></input></div>").appendTo(rootDiv);
    	}
    	var submit = $("<input type='button' onclick='addPointToServer()' value='确定'/>").appendTo(rootDiv);
//    	console.log(rootDiv.html());
    	var infowindow = new google.maps.InfoWindow(
	    { 
	    	content: rootDiv.html()
	    });
	  	infowindow.open(map,marker);
      }
      
      function addPointToServer(){
      	var values = {geometry:{lat:lastClickPoint.lat(),lng:lastClickPoint.lng()}};
	  	var fields = $(window.event.srcElement).siblings(".field").each(function(index){
	  		var fieldName = $(this).attr("fieldName");
	  		var value = $(this).find("input").val();
	  		values[fieldName] = value;
	  	});
//	  	var sql = "INSERT INTO "+FUSIONTABLEID+" ("+fieldNames.join(",")+") VALUES ('"+values.join("','") +"')";
//	  	sql = encodeURI(sql);
//	  	console.log(sql);
//	  	var url = "https://www.google.com/fusiontables/api/query";
	  	var url = "operator";
	  	url = encodeURI(url);
	  	var feature = encodeURI(JSON.stringify(values));
		$.post(
		 	url,
		 	{
		 		 method : "insert",
		 		 feature : feature
		 	},
		 	function(response){
		 		alert(response);
		 	}
		 );	  	
	  	
	  }
      
      
      
      
      